﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

/// <summary>
///WaterMaker 的摘要说明
/// </summary>
public class WaterMaker
{
    public WaterMaker()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    public static void WaterText(string filepath, string text)
    {
        //gif不水印 
        int i = filepath.LastIndexOf(".");
        string ex = filepath.Substring(i, filepath.Length - i);
        if (string.Compare(ex, ".gif", true) == 0)
        {
            return;
        }
        string modifyimagepath = filepath;//修改的图像路径 
        int lucencypercent = 25;
        Image modifyimage = null;
        //Image drawedimage = null;
        Graphics g = null;
        try
        {
            //建立图形对象 
            modifyimage = Image.FromFile(modifyimagepath, true);            
            //drawedimage = Image.FromFile(waterfile, true);
            g = Graphics.FromImage(modifyimage);
            //获取要绘制图形坐标 
            //int x = modifyimage.Width - drawedimage.Width;
            //int y = modifyimage.Height - drawedimage.Height; 
            int x = modifyimage.Width/2-170/2;
            int y = modifyimage.Height/2-20;

            int fontsize = 30;
            int maxSize = Math.Max(modifyimage.Width, modifyimage.Height);
            
            Font font=new Font("Arial",fontsize);
            g.DrawString(text,font,Brushes.LightGray,x,y);            
            //保存文件 
            string[] allowimagetype = { ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
            FileInfo fi = new FileInfo(modifyimagepath);
            ImageFormat imagetype = ImageFormat.Gif;
            switch (fi.Extension.ToLower())
            {
                case ".jpg":
                    imagetype = ImageFormat.Jpeg;
                    break;
                case ".gif":
                    imagetype = ImageFormat.Gif;
                    break;
                case ".png":
                    imagetype = ImageFormat.Png;
                    break;
                case ".bmp":
                    imagetype = ImageFormat.Bmp;
                    break;
                case ".tif":
                    imagetype = ImageFormat.Tiff;
                    break;
                case ".wmf":
                    imagetype = ImageFormat.Wmf;
                    break;
                case ".ico":
                    imagetype = ImageFormat.Icon;
                    break;
                default: break;
            }
            MemoryStream ms = new MemoryStream();
            modifyimage.Save(ms, imagetype);
            byte[] imgdata = ms.ToArray();
            modifyimage.Dispose();
            //drawedimage.Dispose();
            g.Dispose();
            FileStream fs = null;
            //File.Delete(modifyimagepath); 
            fs = new FileStream(modifyimagepath, FileMode.Create, FileAccess.Write);
            if (fs != null)
            {
                fs.Write(imgdata, 0, imgdata.Length);
                fs.Close();
            }
        }
        catch (Exception ex1)
        {
            throw ex1;
        }
        finally
        {
            try
            {
                //drawedimage.Dispose();
                modifyimage.Dispose();
                g.Dispose();
            }
            catch
            { }
        }
    }

    /// <summary> 
    /// 给图片上水印 
    /// </summary> 
    /// <param name="filepath">原图片地址</param> 
    /// <param name="waterfile">水印图片地址</param> 
    /// 
    public static void WaterImg(string filepath, string waterfile)
    {
        //gif不水印 
        int i = filepath.LastIndexOf(".");
        string ex = filepath.Substring(i, filepath.Length - i);
        if (string.Compare(ex, ".gif", true) == 0)
        {
            return;
        }
        string modifyimagepath = filepath;//修改的图像路径 
        int lucencypercent = 25;
        Image modifyimage = null;
        Image drawedimage = null;
        Graphics g = null;
        try
        {
            //建立图形对象 
            modifyimage = Image.FromFile(modifyimagepath, true);
            drawedimage = Image.FromFile(waterfile, true);
            g = Graphics.FromImage(modifyimage);
            //获取要绘制图形坐标 
            int x = modifyimage.Width - drawedimage.Width;
            int y = modifyimage.Height - drawedimage.Height; //设置颜色矩阵 
            float[][] matrixitems = { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, (float)lucencypercent / 100f, 0 }, new float[] { 0, 0, 0, 0, 1 } };
            ColorMatrix colormatrix = new ColorMatrix(matrixitems);
            ImageAttributes imgattr = new ImageAttributes();
            imgattr.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //绘制阴影图像 
            g.DrawImage(drawedimage, new Rectangle(x, y, drawedimage.Width, drawedimage.Height), 10, 10, drawedimage.Width, drawedimage.Height, GraphicsUnit.Pixel, imgattr);
            //保存文件 
            string[] allowimagetype = { ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
            FileInfo fi = new FileInfo(modifyimagepath);
            ImageFormat imagetype = ImageFormat.Gif;
            switch (fi.Extension.ToLower())
            {
                case ".jpg":
                    imagetype = ImageFormat.Jpeg;
                    break;
                case ".gif":
                    imagetype = ImageFormat.Gif;
                    break;
                case ".png":
                    imagetype = ImageFormat.Png;
                    break;
                case ".bmp":
                    imagetype = ImageFormat.Bmp;
                    break;
                case ".tif":
                    imagetype = ImageFormat.Tiff;
                    break;
                case ".wmf":
                    imagetype = ImageFormat.Wmf;
                    break;
                case ".ico":
                    imagetype = ImageFormat.Icon;
                    break;
                default: break;
            }
            MemoryStream ms = new MemoryStream();
            modifyimage.Save(ms, imagetype);
            byte[] imgdata = ms.ToArray();
            modifyimage.Dispose();
            drawedimage.Dispose();
            g.Dispose();
            FileStream fs = null;
            //File.Delete(modifyimagepath); 
            fs = new FileStream(modifyimagepath, FileMode.Create, FileAccess.Write);
            if (fs != null)
            {
                fs.Write(imgdata, 0, imgdata.Length);
                fs.Close();
            }
        }
        catch (Exception ex1)
        {
            throw ex1;
        }
        finally
        {
            try
            {
                drawedimage.Dispose();
                modifyimage.Dispose();
                g.Dispose();
            }
            catch
            { }
        }
    }
}
